package com.tony.pmp.server.controller;

import com.google.common.collect.Maps;
import com.tony.pmp.common.response.BaseResponse;
import com.tony.pmp.common.response.StatusCode;
import com.tony.pmp.common.utils.PageUtil;
import com.tony.pmp.common.utils.ValidatorUtil;
import com.tony.pmp.model.entity.SysPostEntity;
import com.tony.pmp.server.service.SysPostService;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: SysPostController
 * @Auther: 皮蛋布丁
 * @Date: 2021/07/14/21:41
 * @Description: 岗位管理
 */
@RestController
@RequestMapping("/sys/post")
public class SysPostController extends AbstractController {

    @Autowired
    private SysPostService sysPostService;

    /**
    * @Description: list 获取岗位列表数据(分页)
    * @Param: [paramMap]
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/7/14 21:47
    */
    @RequestMapping("/list")
    @RequiresPermissions(value = {"sys:post:list"})
    public BaseResponse list(@RequestParam Map<String,Object> paramMap) {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        Map<String,Object> resMap = Maps.newHashMap();
        try {
            PageUtil page = sysPostService.queryPage(paramMap);
            resMap.put("page",page);

        } catch (Exception e) {
            return new BaseResponse(StatusCode.Fail);
        }
        response.setData(resMap);
        return response;
    }

    /**
    * @Description: save 新增岗位信息
    * @Param: [entity]
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/7/17 22:58
    */
    @RequestMapping(value = "/save",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequiresPermissions(value = {"sys:post:save"})
    public BaseResponse save(@RequestBody @Validated SysPostEntity entity, BindingResult result) {
        String res = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(res)) {
            return new BaseResponse(StatusCode.InvalidParams.getCode(),res);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            log.info("新增岗位信息为：",entity);
            sysPostService.savePost(entity);
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail);
        }
        return response;
    }

    /**
    * @Description: info 获取岗位信息详情
    * @Param: [id]
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/7/18 22:21
    */
    @RequestMapping(value = "/info/{id}",method = RequestMethod.GET)
    @RequiresPermissions(value = {"sys:post:info"})
    public BaseResponse info(@PathVariable Long id) {
        if (id == null || id <= 0) {
            return new BaseResponse(StatusCode.InvalidParams);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        Map<String,Object> resMap = Maps.newHashMap();
        try {
            log.info("修改岗位id为：{}",id);
            resMap.put("post",sysPostService.getById(id));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail);
        }
        response.setData(resMap);
        return response;
    }

    /**
    * @Description: update 更新岗位信息
    * @Param: [entity, result]
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/7/18 22:44
    */
    @RequestMapping(value = "/update",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequiresPermissions(value = {"sys:post:update"})
    public BaseResponse update(@RequestBody @Validated SysPostEntity entity, BindingResult result) {
        String res = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(res)) {
            return new BaseResponse(StatusCode.InvalidParams.getCode(),res);
        }
        if (entity.getPostId() == null || entity.getPostId() == 0) {
            return new BaseResponse(StatusCode.InvalidParams);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            log.info("修改岗位信息为：{}",entity);
            sysPostService.updatePost(entity);
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail);
        }
        return response;
    }

    /**
    * @Description: delete 批量删除岗位
    * @Param: [ids]
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/7/18 23:06
    */
    @RequestMapping(value = "/delete",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequiresPermissions(value = {"sys:post:delete"})
    public BaseResponse delete(@RequestBody Long[] ids) {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            log.info("删除岗位id为：{}", Arrays.asList(ids));
            sysPostService.deletePatch(ids);
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail);
        }
        return response;
    }

    /**
    * @Description: select 获取岗位列表
    * @Param: []
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/7/26 16:40
    */
    @RequestMapping("/select")
    public BaseResponse select() {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            log.info(">>>>>>开始岗位信息的获取>>>>>>");
            Map<String,Object> resMap = Maps.newHashMap();
            resMap.put("list",sysPostService.list());
            response.setData(resMap);
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }



}
